﻿using QldtSdh.GUI.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;

namespace QldtSdh.GUI.DuLieu
{
    public partial class FormQuanLyChuongTrinhDaoTao_BoSungHocPhan : BaseDialog
    {
        public event DanhSachHocPhanAddedHandler OnHocPhanAdded = null;
        private List<ChuongTrinhDaoTao_HocPhan> m_HocPhanList;
        private ChuongTrinhDaoTao m_ChuongTrinhDaoTao;
        private bool _DoNotUpdateGuiBySelectedDonVi = false;

        public FormQuanLyChuongTrinhDaoTao_BoSungHocPhan(ChuongTrinhDaoTao ct)
        {
            InitializeComponent();
            gridData.AddRowIndexing();

            m_ChuongTrinhDaoTao = ct;
            if (ct != null)
            {
                m_HocPhanList = BizChuongTrinhDaoTao_HocPhan.GetListWithHocPhanDetails(ct.MaChuongTrinhDaoTaoGUID);
                lbTieuDe.Text = string.Format(lbTieuDe.Text, ct.TenChuongTrinhDaoTao);

                gridData.SetDataGridViewAsReadonly(rowHeadersVisible: true, columnHeadersVisible: true);
                cmbHocKy.SelectedIndex = 0;

                cmbKhoiKienThuc.Items.Clear();
                cmbKhoiKienThuc.Items.AddRange(BizKhoiKienThuc.List.ToArray<string>());
                cmbKhoiKienThuc.SelectedIndex = 0;

                _DoNotUpdateGuiBySelectedDonVi = true;
                var ls = BizDonViDaoTao.GetList();
                cmbDonVi.DataSource = ls;
                cmbDonVi.DisplayMember = "TenDonVi";
                cmbDonVi.ValueMember = "MaDonViGUID";
                _DoNotUpdateGuiBySelectedDonVi = false;
                UpdateGuiBySelectedDonVi();
            }
        }

        private bool _DoNotUpdateGuiBySelectedHocPhan = false;

        private void UpdateGuiBySelectedDonVi()
        {
            _DoNotUpdateGuiBySelectedHocPhan = true;
            var donVi = cmbDonVi.SelectedItem as DonVi;
            if (donVi != null)
            {
                var lstHocPhan = BizHocPhan.GetList(donVi.MaDonViGUID);
                for (int i = lstHocPhan.Count - 1; i >= 0; i--)
                {
                    var daTonTaiTrongCTDT = false;
                    foreach (var x in m_HocPhanList)
                        if (x.MaHocPhan == lstHocPhan[i].MaHocPhanGUID)
                        {
                            daTonTaiTrongCTDT = true;
                            break;
                        }
                    if (daTonTaiTrongCTDT)
                        lstHocPhan.RemoveAt(i);
                }
                bdHocPhanDuocChon.DataSource = lstHocPhan;
            }
            else
            {
                bdHocPhanDuocChon.DataSource = null;
            }
            gridData.SetDataSource(bdHocPhanDuocChon);
            _DoNotUpdateGuiBySelectedHocPhan = false;
            UpdateGuiBySelectedHocPhan();
        }

        private void UpdateGuiBySelectedHocPhan()
        {
            HocPhan hp = null;
            if (bdHocPhanDuocChon.Current != null)
                hp = bdHocPhanDuocChon.Current as HocPhan;
            if (hp != null)
            {
                //HocPhan_PropertyGrid hocPhanProperty = new HocPhan_PropertyGrid()
                //{
                //    MaHocPhan = hp.MaHocPhan,
                //    TenHocPhan = hp.TenHocPhan,
                //    HinhThucHoc = hp.HinhThucHoc,
                //    HinhThucThi = hp.HinhThucThi,
                //    DonViPhuTrach = BizDonViDaoTao.GetByID(hp.MaDonViPhuTrachGUID).TenDonVi,
                //    DiemQTHT = hp.HeSoDiemQTHT.ToString(),
                //    DiemThi = (100 - hp.HeSoDiemQTHT).ToString(),
                //    SoTinChi = hp.SoTinChi.ToString(),
                //    TongSoGio = hp.TongSoGio.ToString(),
                //    GhiChu = hp.GhiChu
                //};
                //propertyGridHocPhan.SelectedObject = hocPhanProperty;
                txtTenHocPhan.Text = string.Format("{0} - {1} ({2}TC)", hp.MaHocPhan, hp.TenHocPhan, hp.SoTinChi);
                btnThemVaoCTDT.Enabled = true;
            }
            else
            {
                //propertyGridHocPhan.SelectedObject = null;
                txtTenHocPhan.Text = "";
                btnThemVaoCTDT.Enabled = false;
            }
        }

        private void bdHocPhanDuocChon_CurrentChanged(object sender, EventArgs e)
        {
            if (_DoNotUpdateGuiBySelectedHocPhan)
                return;
            UpdateGuiBySelectedHocPhan();
        }

        private void cmbDonVi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_DoNotUpdateGuiBySelectedDonVi)
                return;
            UpdateGuiBySelectedDonVi();
        }

        private void btnThemVaoCTDT_Click(object sender, EventArgs e)
        {
            HocPhan hp = null;
            if (bdHocPhanDuocChon.Current != null)
                hp = bdHocPhanDuocChon.Current as HocPhan;
            if (hp != null)
            {
                ChuongTrinhDaoTao_HocPhan obj = new ChuongTrinhDaoTao_HocPhan();
                obj.MaChuongTrinhDaoTaoGUID = m_ChuongTrinhDaoTao.MaChuongTrinhDaoTaoGUID;
                obj.MaHocPhan = hp.MaHocPhanGUID;
                obj.LaHocPhanBatBuoc = chkLaHocPhanBatBuoc.Checked;
                obj.HocKy = int.Parse(cmbHocKy.Text);
                obj.KhoiKienThuc = cmbKhoiKienThuc.SelectedIndex;
                try
                {
                    BizChuongTrinhDaoTao_HocPhan.Add(obj);
                    m_HocPhanList.Add(obj);
                    obj.HocPhan = hp;
                    if (OnHocPhanAdded != null)
                        OnHocPhanAdded(obj);
                    _DoNotUpdateGuiBySelectedHocPhan = true;
                    try
                    {
                        bdHocPhanDuocChon.RemoveCurrent();
                    }
                    catch { }
                    _DoNotUpdateGuiBySelectedHocPhan = false;
                    UpdateGuiBySelectedHocPhan();
                }
                catch (Exception exc)
                {
                    DialogUtils.ShowErrorWithDefaultTitle("Không thể bổ sung học phần {0} vào chương trình đào tạo được.\r\n{1}", hp.TenHocPhan, exc.Message);
                }
            }

        }

        public delegate void DanhSachHocPhanAddedHandler(ChuongTrinhDaoTao_HocPhan hocPhan);
    }
}
